<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_tyeid/model')->方法名();
*/
namespace jy_tyeid;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function checkinfo($params=array(),$noexi=false){
		$setting=plugin('jy_tyeid')->plugin_setting('basic');
		if(!$setting['isopen']){
		    if(!$noexi){
                exi('实名核实未启用！','error');
            }else{
		        return false;
            }
        }
		if(!$setting['appkey'] || !$setting['appsecret'] || !$setting['appcode']){
			exi('请联系管理员，设置基本参数！','error');
		}
		$api_url='https://eid.shumaidata.com/eid/check';
		$post=array(
			'idcard'=>$params['cardnum'],
			'name'=>urlencode($params['realname']),
		);
        $querys = "idcard=".$post['idcard']."&name=".$post['name'];
		$res=$this->request($api_url,$querys);
        return $res;
    }
    public function request($api_url,$querys='',$msg=false){
        $setting=plugin('jy_tyeid')->plugin_setting('basic');
        if(!$setting['appkey'] || !$setting['appsecret'] || !$setting['appcode']){
            exi('请联系管理员，设置基本参数！','error');
        }
        $method = "POST";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $setting['appcode']);
        $bodys = "";
        $url = $api_url . "?" . $querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设定返回信息中是否包含响应信息头，启用时会将头文件的信息作为数据流输出，true 表示输出信息头, false表示不输出信息头
        //如果需要将字符串转成json，请将 CURLOPT_HEADER 设置成 false
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$api_url, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $data=curl_exec($curl);
        $res=cfc('http')->ihttp_response_parse($data);
        $res=json_decode($res['content'],true);
        if($res['code']){
            if($msg){
                return error($res['message']);
            }else{
                exi('核实身份失败:'.$res['message'].'，'.$res['code'],'error');
            }
        }
        return $res;
    }
}
?>